package com.example.jin.myapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;

import com.example.jin.myapplication.database.CrimeDatabaseHelper;
import com.example.jin.myapplication.database.CrimeDbSchema;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by jin on 2016/5/30.
 */
public class CrimeList {

    private static CrimeList _instance=null;
    private SQLiteDatabase mDatabase;
    private Context mContext;
    public static CrimeList Instance(Context context){
        if(_instance==null){
            _instance=new CrimeList(context);
        }
        return _instance;
    }
    public Crime getCrime(UUID id){
        Crime crime=null;
        CrimeCursorWraper wraper = queryCrimes(CrimeDbSchema.CrimeTable.Cols.UUID + "=?", new String[]{id.toString()});
        try {
            if (wraper.moveToNext()) {
                crime = wraper.getCrime();
            }
        }
        finally {
            wraper.close();
        }
        return crime;
    }
    public File getPhotoFile(Crime crime)
    {
        File dir=mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        Log.i("main","dir:"+dir.toString()+" "+crime.getPictureName());
        return new File(dir, crime.getPictureName());
    }
    public List<Crime> getCrimes(){
        List<Crime> crimes=new ArrayList<>();
        CrimeCursorWraper wraper=queryCrimes(null,null);
        try {
            while (wraper.moveToNext()) {
                Crime crime = wraper.getCrime();
                crimes.add(crime);
            }
        }
        finally {
            wraper.close();
        }
        return crimes;
    }
    public int getPosition(UUID id){
        List<Crime> crimes=getCrimes();
        int ret=0;
        for(Crime crime:crimes){
            if(crime.getId().equals(id)){
                return ret;
            }
            ret++;
        }
        return 0;
    }

    public void addCrime(Crime crime){
        mDatabase.insert(CrimeDbSchema.CrimeTable.NAME,null,getContentValues(crime));
    }
    public void updateCrime(Crime crime){
        String uuidString=crime.getId().toString();
        ContentValues values=getContentValues(crime);
        mDatabase.update(CrimeDbSchema.CrimeTable.NAME,values,CrimeDbSchema.CrimeTable.Cols.UUID+"=?",new String[]{uuidString});
    }
    private CrimeCursorWraper queryCrimes(String whereClause,String[] whereArg){
        Cursor cursor= mDatabase.query(CrimeDbSchema.CrimeTable.NAME,null,whereClause,whereArg,null,null,null);
        CrimeCursorWraper wrapper=new CrimeCursorWraper(cursor);
        return wrapper;
    }

    private CrimeList(Context context){
        mContext=context.getApplicationContext();
        mDatabase=new CrimeDatabaseHelper(mContext).getWritableDatabase();
    }

    private ContentValues getContentValues(Crime crime){
        ContentValues values=new ContentValues();
        values.put(CrimeDbSchema.CrimeTable.Cols.UUID,crime.getId().toString());
        values.put(CrimeDbSchema.CrimeTable.Cols.TITLE,crime.getTitle());
        values.put(CrimeDbSchema.CrimeTable.Cols.DATE,crime.getDate().getTime());
        values.put(CrimeDbSchema.CrimeTable.Cols.SOLVED,crime.isSolved());
        return values;
    }
    public class CrimeCursorWraper extends CursorWrapper{

        /**
         * Creates a cursor wrapper.
         *
         * @param cursor The underlying cursor to wrap.
         */
        public CrimeCursorWraper(Cursor cursor) {
            super(cursor);
        }
        public Crime getCrime(){

            String uuid=getString(getColumnIndex(CrimeDbSchema.CrimeTable.Cols.UUID));
            long date=getLong(getColumnIndex(CrimeDbSchema.CrimeTable.Cols.DATE));
            String title=getString(getColumnIndex(CrimeDbSchema.CrimeTable.Cols.TITLE));
            int sovled=getInt(getColumnIndex(CrimeDbSchema.CrimeTable.Cols.SOLVED));
            Crime crime=new Crime(UUID.fromString(uuid));
            crime.setDate(new Date(date));
            crime.setTitle(title);
            crime.setSolved(sovled!=0);
            return crime;
        }
    }
}
